package uk.co.penguinpower.yasl;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class newListDialog extends Dialog implements OnClickListener {
	Button btnOK;
	Button btnCancel;
	EditText txtListName;
	Spinner cmbNewListStore;
	
	DBManager db;

	public newListDialog(Context context) {
		super(context);
		setContentView(R.layout.new_list_dlg);
		
		db = new DBManager(this.getContext());
		
		this.setTitle("@string/create_new_list");
		
		btnOK = (Button) findViewById(R.id.btnOK);
		btnOK.setOnClickListener(this);
		
		btnCancel = (Button) findViewById(R.id.btnCancel);
		btnCancel.setOnClickListener(this);
		
		txtListName = (EditText) findViewById(R.id.txtListName);
		
		//fill storez with store names and stick it in cmbNewListStore
		String storez[] = {};
		storez = db.getAllStores().toArray(storez);
        
        ArrayAdapter<String> storezAdapter = new ArrayAdapter<String> (context, android.R.layout.simple_spinner_item, storez);
        cmbNewListStore = (Spinner) findViewById(R.id.cmbNewListStore);
        cmbNewListStore.setAdapter(storezAdapter);
        
	}

	@Override
	public void onClick(View v) {
		if (v == btnOK) {
			String s = txtListName.getText().toString();
			db.addList(s, db.getStoreID(cmbNewListStore.getSelectedItem().toString()));
			db.createListTable(s);
			//refresh the list selector
			YASL.refreshListz();
			
			//clear the form
			txtListName.setText("");
			cmbNewListStore.setSelection(0);

			dismiss();
		} else {
			if (v == btnCancel) {
				//clear the form
				txtListName.setText("");
				cmbNewListStore.setSelection(0);
				
				cancel();
			}
		}
	}

}